/*****************************************************************************/
/**
 *  @file   SPHFileToAVSField.cpp
 *  @author Naohisa Sakamoto
 */
/*----------------------------------------------------------------------------
 *
 *  Copyright (c) Visualization Laboratory, Kyoto University.
 *  All rights reserved.
 *  See http://www.viz.media.kyoto-u.ac.jp/kvs/copyright/ for details.
 *
 *  $Id$
 */
/*****************************************************************************/
#include "SPHFileToAVSField.h"
#include <kvs/Math>
#include <kvs/Vector3>
#include <kvs/ValueArray>
#include "SPHFile.h"


namespace kvsbloodvis
{

namespace util
{

/*===========================================================================*/
/**
 *  @brief  Returns AVS field data as unchar-type regular grid in range of 0 to 255.
 *  @param  sph [in] SPH file
 *  @return AVS field data
 */
/*===========================================================================*/
kvs::AVSField SPHFileToAVSField( const SPHFile& sph )
{
    const kvs::Vector3ui resolution = sph.resolution();
    const kvs::UInt32 values_size = resolution.x() * resolution.y() *resolution.z();
    kvs::ValueArray<kvs::UInt8> values( values_size );
    for ( size_t i = 0; i < values.size(); i++ )
    {
        const kvs::Real64 value = kvs::Math::Clamp( sph.data().at<kvs::Real64>(i), 0.0, 1.0 );
        values[i] = kvs::Math::Round( 255.0 * value );
    }

    kvs::AVSField avs;
    avs.setBits( 8 );
    avs.setSigned( false );
    avs.setVeclen( 1 );
    avs.setNSpace( 3 );
    avs.setNDim( 3 );
    avs.setDim( resolution );
    avs.setFieldType( kvs::AVSField::Uniform );
    avs.setDataType( kvs::AVSField::Byte );
    avs.setValues( values );

    return avs;
}

} // end of namespace util

} // end of namespace kvsbloodvis
